package test1.practice;

import java.util.Random;
import java.util.Scanner;
//号码由6个红球和1个蓝球组成。红球的范围是1~33，不允许重复。篮球是1~16,允许重复;
public class lotteryticket {
    public static void main(String[] args) {
        int[] arr = creatnumber();
        System.out.println("======================================");
        //生成中奖号码;
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println("======================================");
        int[] userInputArr = userInputNumber();
        int redCount = 0;
        int blueCount = 0;
        for (int i = 0; i < userInputArr.length - 1; i++) {
            int redNumber = userInputArr[i];
            for (int j = 0; j < arr.length - 1; j++) {
                if (redNumber == arr[j]) {
                    redCount++;
                    //如果找到了，后面的数字就没有必要继续比较了
                    //跳出内循环，继续判断下一个红球号码是否中奖
                    break;
                }
            }
        }
        int blueNumber = userInputArr[userInputArr.length - 1];
        if (blueNumber == arr[arr.length - 1]) {
            blueCount++;
        }
        //System.out.println(redCount);// 红球中奖号码
        //System.out.println(blueCount);//蓝球中奖号码
        //根据红球个数以及篮球的个数来判断中奖情况
        if (redCount == 6 && blueCount == 1) {
            System.out.println("恭喜你中奖1000万");
        } else if (redCount == 6 && blueCount == 0) {
            System.out.println("恭喜你中奖500万");
        } else if (redCount == 5 && blueCount == 1) {
            System.out.println("恭喜你中奖3000元");
        } else if ((redCount == 5 && blueCount == 0) || (redCount == 4 && blueCount == 1)) {
            System.out.println("恭喜你中奖200元");
        } else if ((redCount == 4 && blueCount == 0) || (redCount == 3 && blueCount == 1) ){
            System.out.println("恭喜你中奖10元");
        } else if ((redCount == 0 && blueCount == 1) || (redCount == 2 && blueCount == 1) || (redCount == 1 && blueCount == 1)) {
            System.out.println("恭喜你中奖5元");
        } else {
            System.out.println("谢谢参与！");
        }
    }
    public static int[]userInputNumber(){
        //创建数组用来添加用户购买的彩票号码;
        //6个红球,1个蓝球;数组长度为7;
        int []arr=new int[7];
        Scanner sc=new Scanner(System.in);
        for (int i = 0; i < 6; ) {
            System.out.println("请输入第"+(i+1)+"个红球号码");//i是从0开始的，所以+1;
            int redNumber=sc.nextInt();
            if (redNumber>=1&&redNumber<=33){
                Boolean flag=contains(arr,redNumber);//检验唯一性;
                if (!flag){
                    arr[i]=redNumber;
                    i++;
                }else {
                    System.out.println("当前号码已存在，请重新输入");
                }
            }else {
                System.out.println("当前号码超出范围");
            }
        }
        System.out.println("请输入蓝球号码");
        while (true) {
            int blueNumber=sc.nextInt();
            if (blueNumber >= 1 && blueNumber <= 16) {

                arr[arr.length-1]=blueNumber;
                break;
            }else {
                System.out.println("当前蓝球号码超出范围");
            }
        }
        return arr;
    }
    public static int[] creatnumber(){
        int[]arr=new int[7];
        Random r=new Random();
        for (int i = 0; i < 6;) {
            //获取红球号码
           int redNumber= r.nextInt(33)+1;//从1到33;
           boolean flag=contains(arr,redNumber);
           if (!flag){
               //将红球号码添加到数组中
               arr[i]=redNumber;
               i++;
           }
        }
        //生成蓝球号码并添加到数组中
        int blueNumber=r.nextInt(16)+1;
        arr[arr.length-1]=blueNumber;
        return arr;
    }
    //下一个方法的作用为判断数字在数组中是否存在
    public static boolean contains(int[]arr,int number){
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]==number){
                return true;
            }
        }
        return false;
    }
}
